#include <stdlib.h>
#include <stdio.h>
#include "awf_comparison.h"


int main(int argc, char **argv)
{
	char *image_path = argv[1];
	char *model_path = argv[2];

	AWF_Comparison_Container *compr_contaioner = (AWF_Comparison_Container *)malloc(sizeof(AWF_Comparison_Container));
	AWF_Face_Database *database = (AWF_Face_Database *)malloc(sizeof(AWF_Face_Database));

	int feat_dim = 128;
	float thresh = 0.5;

	// Make containers.
	awf_make_comparison_container(compr_contaioner, database, feat_dim, thresh, model_path);

	float qa_score = 44;
	float confidence = 0.845;
	short cx[5] = { 89, 133, 108, 88, 121 };
	short cy[5] = { 103, 108, 132, 148, 153 };
	int ldmk_num = 5;
	for (int lcnt = 0; lcnt < 10; lcnt++)  // cycle for 100 times
	{
		AW_Img rgb_img = aw_load_image_stb(image_path, 3);
		AW_Box face_box;
		face_box.tl_x = 56;
		face_box.tl_y = 60;
		face_box.br_x = 168;
		face_box.br_y = 180;

		face_box.width = face_box.br_x - face_box.tl_x;
		face_box.height = face_box.br_y - face_box.tl_y;
		face_box.area = face_box.width * face_box.height;

		AW_LDMK face_ldmk = aw_make_ldmk(ldmk_num);
		for (int tcnt = 0; tcnt < face_ldmk.num; tcnt++)
		{
			face_ldmk.cx[tcnt] = cx[tcnt];
			face_ldmk.cy[tcnt] = cy[tcnt];
		}

		float *img_feat = (float *)malloc(feat_dim * sizeof(float));
		int similar_cnt = 0;
		int *similar_indices = (int *)malloc(MAX_SIMILAR_NUM * sizeof(int));
		float *dists = (float *)malloc(MAX_SIMILAR_NUM * sizeof(float));
		int flag = awf_run_face_comparsion(compr_contaioner, database, &rgb_img, &face_box, &face_ldmk, img_feat, &similar_cnt, similar_indices, dists);
		if (flag == 1)  // add the curr unit to the database
		{
			database->add_unit(database, img_feat);
		}
		else // curr unit is the similarity with the one(more) unit of the database
		{
			for (int tcnt = 0; tcnt < similar_cnt; tcnt++)
			{
				printf("current unit is the simiarity with the id %d, and the dist is %f\n",
					similar_indices[tcnt], dists[tcnt]);
			}
		}

		if (lcnt == 3)
		{
			database->release(database);
		}

		aw_free_image(&rgb_img);
		aw_free_ldmk(&face_ldmk);
		free(similar_indices);
		free(dists);
		free(img_feat);
	}

	// Free memory.
	printf("--------------------> Free memory-----------------------\n");
	awf_free_comparison_container(compr_contaioner, database);
	free(compr_contaioner);
	free(database);
	return 0;
}